package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.DtsUser;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.DtsUserService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(tags = "站点管理员")
@RestController
@RequestMapping("sys/siteAdministrator")
public class SysSiteAdministratorController {

    @Autowired
    private DtsUserService dtsUserService;

    @GetMapping
    @ApiOperation(value = "站点管理员条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的大小"),
            @ApiImplicitParam(name = "name", value = "用户名称"),
    })
    public JsonResult<Page<DtsUser>> findByPage(@ApiIgnore Page<DtsUser> page, String realName) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        page.addOrder(OrderItem.desc("user_regtime"));
        Page<DtsUser> dtsUserPage = dtsUserService.page(page, new LambdaQueryWrapper<DtsUser>()
                .eq(shopId != 1, DtsUser::getShopId, shopId)
                .ne(DtsUser::getUserLevel, 0)
                .eq(!StringUtils.isEmpty(realName), DtsUser::getRealName, realName)
        );
        List<DtsUser> records = dtsUserPage.getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            for (DtsUser record : records) {
                if (!StringUtils.isEmpty(record.getCommunityId())) {
                    String communityName = dtsUserService.getCommunityNameById(record.getCommunityId());
                    record.setCommunityName(communityName);
                }
            }
        }
        return JsonResult.ok(dtsUserPage);
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询站点管理员")
    public JsonResult<DtsUser> getPointAdmin(@PathVariable Long id) {
        DtsUser dtsUser = dtsUserService.getById(id);
        return JsonResult.ok(dtsUser);
    }

    @PostMapping
    @ApiOperation(value = "新增站点管理员")
    public JsonResult addPointAdmin(@RequestBody DtsUser dtsUser) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        dtsUser.setShopId(shopId);
        boolean save = dtsUserService.addDtsUser(dtsUser);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @PutMapping
    @ApiOperation(value = "修改站点管理员")
    public JsonResult putPointAdmin(@RequestBody DtsUser dtsUser) {
        boolean update = dtsUserService.updateDtsUser(dtsUser);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "删除站点管理员")
    public JsonResult delPointAdmin(@PathVariable Long id) {
        boolean remove = dtsUserService.removeById(id);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @PutMapping("changePassword")
    @ApiOperation(value = "重置站点管理员登录密码")
    public JsonResult changePassword(@RequestBody Map<String, String> map) {
        String id = map.get("id");
        String password = map.get("password");
        DtsUser dtsUser = new DtsUser();
        dtsUser.setId(Long.valueOf(id));
        dtsUser.setPassword(new BCryptPasswordEncoder().encode(password));
        boolean update = dtsUserService.updateDtsUser(dtsUser);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @PutMapping("setStatus")
    @ApiOperation(value = "修改站点管理员用户状态")
    public JsonResult setStatus(@RequestBody Map<String, String> map) {
        String id = map.get("id");
        String status = map.get("status");
        DtsUser dtsUser = new DtsUser();
        dtsUser.setId(Long.valueOf(id));
        dtsUser.setStatus(Byte.valueOf(status));
        boolean update = dtsUserService.updateDtsUser(dtsUser);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


}
